home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 10.5 KB | 379 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWPolySh.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWPOLYSH_H
- #include "FWPolySh.h"
- #endif
-
- #ifndef FWGRGLOB_H
- #include "FWGrGlob.h"
- #endif
-
- #ifndef FWGRUTIL_H
- #include "FWGrUtil.h"
- #endif
-
- #ifndef FWGC_H
- #include "FWGC.h"
- #endif
-
- #ifndef FWINK_H
- #include "FWInk.h"
- #endif
-
- #ifndef FWSTYLE_H
- #include "FWStyle.h"
- #endif
-
- #ifndef FWRASTER_H
- #include "FWRaster.h"
- #endif
-
- #ifndef FWPOLY_H
- #include "FWPoly.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _TRANSFORM_
- #include <Trnsform.xh>
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWGraphx_PolygonShape
- #endif
-
- FW_DEFINE_CLASS_M1(FW_CPolygonShape, FW_CShape)
- FW_REGISTER_ARCHIVABLE_CLASS(FW_LPolygonShape, FW_CPolygonShape, FW_CPolygonShape::Read, FW_CShape::Write)
-
- //========================================================================================
- // Polygon hit-testing functions
- //========================================================================================
-
- static FW_CFixed XIntersect(const FW_CPoint* vtx0, const FW_CPoint* vtx1, FW_CFixed ty)
- {
- return
- vtx1->x -
- FW_WideMultiply(vtx1->y - ty, vtx0->x - vtx1->x) /
- (vtx0->y - vtx1->y);
- }
-
- static FW_Boolean IsPointInPolygon(unsigned long pointCount, const FW_CPoint* points, FW_CPoint point)
- {
- FW_Boolean yFlag0, yFlag1, isInside = FALSE, xFlag0;
-
- FW_CFixed tx = point.x;
- FW_CFixed ty = point.y;
-
- const FW_CPoint* vtx0 = points + pointCount - 1;
- const FW_CPoint* vtx1 = points;
-
- yFlag0 = vtx0->y >= ty;
-
- for(unsigned long j = 0; j < pointCount; j ++)
- {
- yFlag1 = vtx1->y >= ty;
-
- if(yFlag1 != yFlag0)
- {
- xFlag0 = vtx0->x >= tx;
-
- if(xFlag0 == (vtx1->x >= tx))
- {
- if(xFlag0)
- isInside = !isInside;
- }
- else
- {
- if(::XIntersect(vtx0, vtx1, ty) >= tx)
- isInside = !isInside;
- }
- }
-
- yFlag0 = yFlag1;
- vtx0 = vtx1;
- vtx1 ++;
- }
-
- return isInside;
- }
-
- //========================================================================================
- // class FW_CPolygonShape
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::FW_CPolygonShape
- //----------------------------------------------------------------------------------------
-
- FW_CPolygonShape::FW_CPolygonShape(const FW_PPolygon& polygon,
- FW_ERenderVerbs renderVerb,
- FW_Boolean autoCloseFrame,
- const FW_PInk& ink,
- const FW_PStyle& style) :
- FW_CShape(renderVerb, ink, style, FW_kNormalFont),
- fPolygon(polygon),
- fAutoCloseFrame(autoCloseFrame)
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::FW_CPolygonShape
- //----------------------------------------------------------------------------------------
-
- FW_CPolygonShape::FW_CPolygonShape(const FW_CPolygonShape& other) :
- FW_CShape(other),
- fPolygon(other.fPolygon),
- fAutoCloseFrame(other.fAutoCloseFrame)
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::FW_CPolygonShape
- //----------------------------------------------------------------------------------------
-
- FW_CPolygonShape::FW_CPolygonShape(FW_CReadableStream& archive) :
- FW_CShape(archive)
- {
- archive >> fPolygon;
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::~FW_CPolygonShape
- //----------------------------------------------------------------------------------------
-
- FW_CPolygonShape::~FW_CPolygonShape()
- {
- FW_START_DESTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CPolygonShape& FW_CPolygonShape::operator=(const FW_CPolygonShape& other)
- {
- fPolygon = other.fPolygon;
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::SetAutoCloseFrame
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonShape::SetAutoCloseFrame(FW_Boolean autoCloseFrame)
- {
- fAutoCloseFrame = autoCloseFrame;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::GetAutoCloseFrame
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CPolygonShape::GetAutoCloseFrame() const
- {
- return fAutoCloseFrame;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::Render
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonShape::Render(FW_CGraphicContext& gc) const
- {
- gc.GetRasterizer()->RenderPolygon(
- gc,
- fPolygon,
- GetRenderVerb(),
- fAutoCloseFrame,
- fInk,
- fStyle);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::HitTest
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CPolygonShape::HitTest(FW_CGraphicContext& gc,
- const FW_CPoint& test,
- FW_CFixed tolerance) const
- {
- FW_UNUSED(gc);
-
- if(fRenderVerb == FW_kNoRendering)
- return FALSE;
-
- // Quickly check the bounds
- FW_CRect bounds;
- fPolygon->GetBounds(bounds);
-
- bounds.Inset(-tolerance, -tolerance);
-
- if(bounds.Contains(test))
- {
- // Bounds check out OK, now do a more thourough test
- unsigned long pointCount = fPolygon->GetCount();
- const FW_CPoint* points = fPolygon->GetPoints();
- if(fRenderVerb == FW_kFrame)
- {
- for(unsigned long i = 0; i < pointCount - 1; i ++)
- if(::FW_HitTestLine(points[i], points[i + 1], test, tolerance))
- return TRUE;
- }
- else
- return ::IsPointInPolygon(pointCount, points, test);
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::Copy
- //----------------------------------------------------------------------------------------
-
- FW_CShape* FW_CPolygonShape::Copy() const
- {
- return FW_NEW(FW_CPolygonShape, (*this));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::Transform
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonShape::Transform(Environment *ev, ODTransform* transform)
- {
- fPolygon->Transform(ev, transform);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::InverseTransform
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonShape::InverseTransform(Environment *ev, ODTransform* transform)
- {
- fPolygon->InverseTransform(ev, transform);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::MoveShape
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonShape::MoveShape(FW_CFixed deltaX, FW_CFixed deltaY)
- {
- fPolygon->Move(deltaX, deltaY);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::MoveShapeTo
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonShape::MoveShapeTo(FW_CFixed x, FW_CFixed y)
- {
- fPolygon->MoveTo(x, y);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::Inset
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonShape::Inset(FW_CFixed x, FW_CFixed y)
- {
- fPolygon->Inset(x, y);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::GetBounds
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonShape::GetBounds(FW_CGraphicContext& gc, FW_CRect& rect) const
- {
- FW_UNUSED(gc);
-
- fPolygon->GetBounds(rect);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::Flatten
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonShape::Flatten(FW_CWritableStream& archive) const
- {
- FW_CShape::Flatten(archive);
-
- archive << fPolygon;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::RenderPolygon
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonShape::RenderPolygon(FW_CGraphicContext& gc,
- const FW_PPolygon& polygon,
- FW_ERenderVerbs renderVerb,
- FW_Boolean autoCloseFrame,
- const FW_PInk& ink,
- const FW_PStyle& style)
- {
- gc.GetRasterizer()->RenderPolygon(
- gc,
- polygon,
- renderVerb,
- autoCloseFrame,
- ink,
- style);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::Read
- //----------------------------------------------------------------------------------------
-
- void* FW_CPolygonShape::Read(FW_CReadableStream& archive)
- {
- return FW_NEW(FW_CPolygonShape, (archive));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::GetGeometry
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonShape::GetGeometry(FW_PPolygon& polygon) const
- {
- polygon = fPolygon;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonShape::SetGeometry
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonShape::SetGeometry(const FW_PPolygon& polygon)
- {
- fPolygon = polygon;
- }
-
-